﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using WebApplication3.Data;

namespace WebApplication3
{
	public class Program
	{
		public static void Main(string[] args)
		{
			var host=CreateWebHostBuilder(args).Build() ;
			
			using (var scope = host.Services.CreateScope())
			{
				var services = scope.ServiceProvider;
				var loggerFactory = services.GetRequiredService<ILoggerFactory>();
				try
				{
					var catalogContext = services.GetRequiredService<Data.ApplicationDbContext>();
					Data.MyDataContexSeed.SeedAsync(catalogContext, loggerFactory)
			.Wait();

					var userManager = services.GetRequiredService<UserManager<Data.Entities.AppIdentityUser>>();
					var roleManager = services.GetRequiredService<RoleManager<Data.Entities.AppIdentityRole>>();

					AppIdentityDbContextSeed.SeedAsync(userManager, roleManager, loggerFactory,10).Wait();
				}
				catch (Exception ex)
				{
					var logger = loggerFactory.CreateLogger<Program>();
					logger.LogError(ex, "An error occurred seeding the DB.");
				}
			}

			host.Run();
		}

		public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
			WebHost.CreateDefaultBuilder(args)
			.UseKestrel()
				.UseStartup<Startup>();
	}
}
